package com.ddshuai.easy;

import java.util.HashSet;
import java.util.Set;

/**
 * 描述
 *
 * @author ddshuai
 * @date 2019-04-18 11:00
 **/
public class DeleteDuplicates {

    public static ListNode deleteDuplicates(ListNode head) {
        if (head == null){
            return head;
        }
        ListNode next = head.next;
        ListNode pre = head;
        Set<Integer> set = new HashSet<>(20);
        set.add(head.val);
        while (next != null) {
            int size = set.size();
            set.add(next.val);
            if (size == set.size()){
                pre.next = next.next;
                next = next.next;
            }else {
                pre = next;
                next = next.next;
            }
        }
        return head;
    }

    static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        ListNode head1 = new ListNode(3);
        head.next = head1;
        head1.next = new ListNode(3);
        ListNode node = deleteDuplicates(head);
        System.out.println(node);
    }
}
